package com.useeinfo.asg.open.pattern.geo;

import java.math.BigDecimal;

/**
 * Created by MISAYA on 2016/9/14.
 */

/***
 * 计算两地距离
 */
public class LocationDistance {

    public static double getDistanceFromXtoY(double lat_a, double lng_a,
                                       double lat_b, double lng_b) {
        double pk = 180 / 3.14169;
        double a1 = lat_a / pk;
        double a2 = lng_a / pk;
        double b1 = lat_b / pk;
        double b2 = lng_b / pk;
        double t1 = Math.cos(a1) * Math.cos(a2) * Math.cos(b1) * Math.cos(b2);
        double t2 = Math.cos(a1) * Math.sin(a2) * Math.cos(b1) * Math.sin(b2);
        double t3 = Math.sin(a1) * Math.sin(b1);
        double tt = Math.acos(t1 + t2 + t3);
        return 6366000 * tt;
    }

    public static double getDistanceKm(double lat_a, double lng_a,
                                       double lat_b, double lng_b) {

        return Double.valueOf(BigDecimal.valueOf(LocationDistance.getDistanceFromXtoY(lat_a, lng_a, lat_b, lng_b)).divide(new BigDecimal(1000)).setScale(2, BigDecimal.ROUND_HALF_UP).toString());
    }
}
